package gwtadmin.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

public class LeftMenu extends VerticalPanel implements ClickHandler{
	
	Hyperlink hlHome;
	
	Label     hlList;
	Hyperlink hlListWsdl;
	Hyperlink hlListSchema;
	Hyperlink hlListPolicy;
	
	Hyperlink hlWsdlTree;
	
	Label     hlUpload;
	Hyperlink hlUploadWsdl;
	Hyperlink hlUploadSchema;
	Hyperlink hlUploadPolicy;
	
	Hyperlink hlSearch = new Hyperlink("Search", "");
	
	public LeftMenu(IWsdlManager wsdlManager) {
		
		wsdlMan = wsdlManager;
		
		hlHome = createItem("Home", false);
		
		hlList = new Label("List");
		hlListWsdl = createItem("WSDL", true);
		hlListSchema = createItem("Schema", true);
		hlListPolicy = createItem("Policy", true);
		
		hlWsdlTree = createItem("WSDL Tree", false);
		
		hlUpload = new Label("Upload");
		hlUploadWsdl = createItem("WSDL", true);
		hlUploadSchema = createItem("Schema", true);
		hlUploadPolicy = createItem("Policy", true);
		
		hlSearch = createItem("Search", false);
		
		hlHome.setStylePrimaryName("leftMenuItem");
		hlList.setStylePrimaryName("leftMenuItem");
		hlUpload.setStylePrimaryName("leftMenuItem");
		
		setSel(hlHome);
		
		addStyleName("decorFrame");
		addStyleName("leftPanel");

		add(hlHome);
		add(hlList);
		add(hlListWsdl);
		add(hlListSchema);
		add(hlListPolicy);
		add(hlWsdlTree);
		add(hlUpload);
		add(hlUploadWsdl);
		add(hlUploadSchema);
		add(hlUploadPolicy);
		add(hlSearch);
	}
	
	private Hyperlink createItem(String text, boolean sub) {
		Hyperlink hl = new Hyperlink(text, "");
		hl.addClickHandler(this);
		hl.setStylePrimaryName("leftMenuItem");
		
		if (sub)
			hl.addStyleDependentName("sub");
		else
			hl.addStyleDependentName("norm");
		
		return hl;
	}

	@Override
	public void onClick(ClickEvent event) {
		
		Hyperlink hl = (Hyperlink)event.getSource();
		
		if (hl == hlListWsdl) {
			wsdlMan.goListWsdl();
		} else if (hl == hlListSchema) {
			wsdlMan.goListSchema();
		} else if (hl == hlListPolicy) {
			wsdlMan.goListPolicy();
		} else if (hl == hlWsdlTree) {
			wsdlMan.goWsdlTree();
		} else if (hl == hlUploadWsdl) {
			wsdlMan.goUploadWsdl();
		} else if (hl == hlUploadSchema) {
			wsdlMan.goUploadSchema();
		} else if (hl == hlUploadPolicy) {
			wsdlMan.goUploadPolicy();
		} else if (hl == hlSearch) {
			wsdlMan.goSearch();
		} else if (hl == hlHome) {
			wsdlMan.goHome();
		}
		
		setSel(hl);
	}
	
	private void setSel(Hyperlink hl) {
		
		if (hlCurrent != null)
			hlCurrent.getElement().getFirstChildElement().removeAttribute("style");
		
		hlCurrent = hl;
		hl.getElement().getFirstChildElement().setAttribute("style", "color: black; text-decoration: none");
	}
	
	private Hyperlink hlCurrent = null;
	private IWsdlManager wsdlMan;
}
